import 'package:cached_network_image/cached_network_image.dart';
import 'package:easydubbing/pages/userInfo.dart';
import 'package:easydubbing/utils/FutureDioToken.dart';
import 'package:easydubbing/utils/SpUtil.dart';
import 'package:easydubbing/utils/api.dart';
import 'package:easydubbing/utils/dateUtil.dart';
import 'package:easydubbing/utils/style.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:loading_indicator_view/loading_indicator_view.dart';

import 'login.dart';

class MyPage extends StatefulWidget{
  _MyPageState createState() => _MyPageState();
}
class _MyPageState extends State<MyPage>{

  String alias;
  String avatar;
  int isPro;
  int isVip;
  bool isLogin;
  Future loadData;
  List historyList = [];

  @override
  void initState() {
    loadData = FutureDio("get", Api.findHistoryList, {
      "uid":SpUtil.sharedPreferences.getInt('uid')
    });
    avatar = SpUtil.sharedPreferences.getString('avatar')??'https://img-blog.csdnimg.cn/20210610163006523.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3loMDAxNg==,size_16,color_FFFFFF,t_70#pic_center';
    isPro = SpUtil.sharedPreferences.getInt('isPro')??0;
    isVip = SpUtil.sharedPreferences.getInt('isVip')??0;
    isLogin = SpUtil.sharedPreferences.getBool('isLogin')??false;
//    未登录
    if(!isLogin){
      alias = SpUtil.sharedPreferences.getString('alias')??'点击登录';
    }else{
      alias = SpUtil.sharedPreferences.getString('alias')??'点击编辑昵称';
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Container(
            height:120,
            decoration: BoxDecoration(
                gradient: LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    colors: [
                      ProjectColor.deepColor,
                      ProjectColor.mainColor,
                      ProjectColor.secColor,
                    ])),
          ),
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              InkWell(
                splashColor: Colors.transparent,
                highlightColor: Colors.transparent,
                onTap: (){
//                  登录跳转修改个人信息
                  if(isLogin){
                    Navigator.push(context,
                        new MaterialPageRoute(builder: (context) => new UserInfo())).then((value) {
                          setState(() {
                            alias=SpUtil.sharedPreferences.getString('alias');
                            avatar = SpUtil.sharedPreferences.getString('avatar')??'https://img-blog.csdnimg.cn/20210610163006523.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3loMDAxNg==,size_16,color_FFFFFF,t_70#pic_center';
                          });
                    });
                  }else{
                    Navigator.push(context,
                        new MaterialPageRoute(builder: (context) => new LoginWidget()));
                  }
                },
                child: Container(
                  height: 80,
                  width:80,
                  margin: EdgeInsets.only(top: 70,left: 20),
                  child: ClipOval(
                    child: CachedNetworkImage(
                      imageUrl: avatar==''?'https://pic1.zhimg.com/50/v2-6afa72220d29f045c15217aa6b275808_hd.jpg?source=1940ef5c':avatar,
                    ),
                  ),
                ),
              ),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  InkWell(
                    onTap: (){
                      if(isLogin){
                      }else{
                        Navigator.push(context,
                            new MaterialPageRoute(builder: (context) => new LoginWidget()));
                      }
                    },
                    child: Container(
                      margin: EdgeInsets.only(top: 100,left: 10),
                      child: Text(alias,style: TextStyle(fontSize: 16,color: Colors.white),),
                    ),
                  ),
                  isPro==1?Row(
                    children: [
                      Container(
                        width: 35,
                        height: 20,
                        margin: EdgeInsets.only(bottom: 10,left: 10,top: 5),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.all(Radius.circular(3)),
                          gradient: LinearGradient(
                            begin: Alignment.centerLeft,
                            end: Alignment.centerRight,
                            colors: [
                              Color(0xFFF8C26A),
                              Color(0xFFFFDB7D),
                            ],
                          ),
                        ),
                        alignment: Alignment.center,
                        child: Text("PRO",style: TextStyle(color: Color(0xFF825C23),fontSize: 13,fontWeight: FontWeight.bold),),
                      ),
                      Container(
                        width: 20,
                        height: 20,
                        margin: EdgeInsets.only(bottom: 10,left: 2,top: 5),
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(3)),
                            color: Colors.red
                        ),
                        alignment: Alignment.center,
                        child: Text("V",style: TextStyle(color: Colors.white,fontSize: 13,fontWeight: FontWeight.bold),),
                      ),
                    ],
                  ):Container(
                    width: 170,
                    height: 20,
                    margin: EdgeInsets.only(bottom: 10,left: 10,top: 5),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(3)),
                      gradient: LinearGradient(
                        begin: Alignment.centerLeft,
                        end: Alignment.centerRight,
                        colors: [
                          Color(0xFFF8C26A),
                          Color(0xFFFFDB7D),
                        ],
                      ),
                    ),
                    alignment: Alignment.center,
                    child: Text("开通PRO会员，享用专业功能 >",style: TextStyle(color: Color(0xFF825C23),fontSize: 11,fontWeight: FontWeight.bold),),
                  ),
                ],
              )
            ],
          ),
          FutureBuilder(
              future: loadData,
              builder: (BuildContext buildContext,AsyncSnapshot snapshot){
                if(snapshot.connectionState==ConnectionState.done) {
                  historyList = snapshot.data.data["data"]??[];
                  return Container(
                    margin: EdgeInsets.only(top: 170, left: 20),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          child: Text(
                              '视频  ${historyList.length}', style: TextStyle(
                              fontSize: 15, fontWeight: FontWeight.bold)),
                        ),
                        SizedBox(
                          height: 5,
                        ),
                        Divider(
                          thickness: 0.6,
                          color: Colors.grey,
                          endIndent: 20,
                          indent: 2,
                          height: 1,
                        ),
                        Expanded(child: ListView.builder(
                            itemCount: historyList.length,
                            itemBuilder: (context, index) {
                              String date = historyList[index]["uploadTime"];
                              String month = dateTransform(date.substring(5,7));
                              String year = date.substring(0,4);
                              String day = date.substring(8,10);
                              return Container(
                                margin: EdgeInsets.only(bottom: 30),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Container(
                                      child: Text(
                                        '${historyList[index]['title']}',
                                        style: TextStyle(
                                            fontWeight: FontWeight.bold,
                                            fontSize: 20),),
                                    ),
                                    Container(
                                      height: 150,
                                      width: 270,
                                      margin: EdgeInsets.only(top: 10),
                                      child: CachedNetworkImage(
                                          fit: BoxFit.cover,
                                          imageUrl: "${historyList[index]['cover']}"
                                      ),
                                    ),

                                    Row(
                                      children: [
                                        Container(
                                          height: 40,
                                          alignment: Alignment.bottomCenter,
                                          child: Row(
                                            children: [
                                              Container(
                                                height: 15,
                                                child: Image.asset(
                                                    "assets/icons/like.png"),
                                              ),
                                              Text(
                                                ' ${historyList[index]['applaud']}  ',
                                                style: TextStyle(
                                                    fontWeight: FontWeight
                                                        .bold),),
                                              Container(
                                                height: 15,
                                                child: Image.asset(
                                                    "assets/icons/favor.png"),
                                              ),
                                              Text(
                                                  ' ${historyList[index]['favor']}  ',
                                                  style: TextStyle(
                                                      fontWeight: FontWeight
                                                          .bold)),
                                              Container(
                                                height: 15,
                                                child: Image.asset(
                                                    "assets/icons/comment.png"),
                                              ),
                                              Text(
                                                  ' ${historyList[index]['comment']}',
                                                  style: TextStyle(
                                                      fontWeight: FontWeight
                                                          .bold)),
                                              Container(
                                                height: 15,
                                                child: Image.asset(
                                                    "assets/icons/icon_see.png"),
                                              ),
                                              Text(
                                                  ' ${historyList[index]['play']}',
                                                  style: TextStyle(
                                                      fontWeight: FontWeight
                                                          .bold)),
                                            ],
                                          ),
                                        ),
                                        Spacer(),
                                        Container(
                                          height: 35,
                                          child: Text('$day',
                                              style: TextStyle(fontSize: 35,)),
                                        ),
                                        Container(
                                          height: 40,
                                          margin: EdgeInsets.only(
                                              left: 5, right: 15),
                                          alignment: Alignment.bottomCenter,
                                          child: Text('$month月 $year',
                                              style: TextStyle(fontSize: 15,
                                                  color: Color(0xff666666))),
                                        ),
                                      ],
                                    ),
                                  ],
                                ),
                              );
                            }

                        ))
                      ],
                    ),
                  );
                }else{
                  return Center(
                    child: Container(
                      width: 50,
                      height: 50,
                      child: LineSpinFadeLoaderIndicator(
                        ballColor: ProjectColor.mainColor,
                      ),
                    ),
                  );
                }
              })
        ],
      ),
    );
  }

}